package cc.avatar.util;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Export2ExcelUtil {
	
	@SuppressWarnings("deprecation")
	public static void export(String[][] data){
		HSSFWorkbook wb = new HSSFWorkbook();		
		HSSFSheet sheet = wb.createSheet("库存表");//新建,命名一个工作单		
		//sheet.setColumnWidth((short)0,(short)2000);
		  
		HSSFRow row = null;
		HSSFCell cell = null;
		
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	
		HSSFCellStyle headerStyle = wb.createCellStyle();
		headerStyle.setFont(font);
	
		for (int i=0 ; i<data.length; i++){
			row = sheet.createRow(i);
		    for (int j=0; j<data[i].length; j++){	    	
		    	cell = getOrCreateCell(row, (short)j);

		    	String s = data[i][j];
		    	if (s != null){
			    	if (i == 0 || j ==0){
			    		cell.setCellStyle(headerStyle);
			    	}
		    	  cell.setCellValue(data[i][j]);
		    	}
		    }
		}
		
		FileOutputStream fileOut;
	    //把生成的文件写到服务器上
		String filename = "d:/lbc_data/库存统计表.xls";
	    try {
		   fileOut = new FileOutputStream(filename);
		   wb.write(fileOut);
	       fileOut.close();
	    } catch (Exception e) {
	       e.printStackTrace();
	    }
	}
	
	private static HSSFCell getOrCreateCell(HSSFRow row, short s) {
		HSSFCell c = row.getCell(s);
		if (c==null)
			c = row.createCell(s);
		
		//c.setCellType(HSSFCell.CELL_TYPE_STRING);
		return c;
	}

}
